package com.cherrypt.api.core.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cherrypt.api.model.dataobject.Post;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * @author intent
 * @date 2019/8/2 15:53
 * @about <link href='http://zzyitj.xyz/'/>
 */
public interface PostDao extends BaseMapper<Post> {

    @Select(value = "select id, created, modify, uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts where is_wait = ${wait} and is_enable = true")
    IPage<Post> selectPostPage(Page page, @Param("wait") boolean wait);

    // 所有post按field升序
    @Select(value = "select id, created, modify, uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts where is_wait = ${wait} and is_enable = true order by hot desc, pin desc, ${field} asc")
    IPage<Post> selectPostPageOrderByAsc(Page page, @Param("field") String field, @Param("wait") boolean wait);

    // 大小降序
    @Select(value = "select sum(size) as size, t_posts.id, t_posts.created, t_posts.modify, t_posts.uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts, t_post_torrent, t_torrents where t_posts.id = t_post_torrent.pid and t_torrents.id = t_post_torrent.tid and is_wait = ${wait} and is_enable = true group by t_posts.id order by pin desc, hot desc, size desc")
    IPage<Post> selectPostPageOrderBySizeDesc(Page page, @Param("wait") boolean wait);

    // 大小升序
    @Select(value = "select sum(size) as size, t_posts.id, t_posts.created, t_posts.modify, t_posts.uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts, t_post_torrent, t_torrents where t_posts.id = t_post_torrent.pid and t_torrents.id = t_post_torrent.tid and is_wait = ${wait} and is_enable = true group by t_posts.id order by pin desc, hot desc, size asc")
    IPage<Post> selectPostPageOrderBySizeAsc(Page page, @Param("wait") boolean wait);

    // 搜索分类
    @Select(value = "select t_posts.id, created, modify, uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts, t_category where t_posts.category_id = t_category.id and t_category.name = '${categoryName}' and is_wait = ${wait} and is_enable = true")
    IPage<Post> selectPostPageByCategoryName(Page page, @Param("categoryName") String categoryName, @Param("wait") boolean wait);

    // 分类按field升序
    @Select(value = "select t_posts.id, created, modify, uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts, t_category where is_enable = true and t_posts.category_id = t_category.id and t_category.name = '${categoryName}' and is_wait = ${wait} and is_enable = true order by hot desc, pin desc, ${field} asc")
    IPage<Post> selectPostPageByCategoryNameOrderByAsc(Page page, @Param("categoryName") String categoryName, @Param("field") String field, @Param("wait") boolean wait);

    // 分类按size降序
    @Select(value = "select sum(size) as size, t_posts.id, t_posts.created, t_posts.modify, t_posts.uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts, t_post_torrent, t_torrents, t_category where t_posts.id = t_post_torrent.pid and t_torrents.id = t_post_torrent.tid and t_posts.category_id = t_category.id and t_category.name = '${categoryName}' and is_wait = ${wait} and is_enable = true group by t_posts.id order by pin desc, hot desc, size desc")
    IPage<Post> selectPostPageByCategoryNameOrderBySizeDesc(Page page, @Param("categoryName") String categoryName, @Param("wait") boolean wait);

    // 分类按size升序
    @Select(value = "select sum(size) as size, t_posts.id, t_posts.created, t_posts.modify, t_posts.uid, category_id, team_id, pin, title, subtitle, hot, imdb_id, douban_id from t_posts, t_post_torrent, t_torrents, t_category where t_posts.id = t_post_torrent.pid and t_torrents.id = t_post_torrent.tid and t_posts.category_id = t_category.id and t_category.name = '${categoryName}' and is_wait = ${wait} and is_enable = true group by t_posts.id order by pin desc, hot desc, size asc")
    IPage<Post> selectPostPageByCategoryNameOrderBySizeAsc(Page page, @Param("categoryName") String categoryName, @Param("wait") boolean wait);

    // 搜索分类总和
    @Select(value = "select count(*) from t_posts, t_category where t_posts.category_id = t_category.id and t_category.name = '${categoryName}' and is_wait = ${wait} and is_enable = true")
    Integer selectCountByCategoryName(@Param("categoryName") String categoryName, @Param("wait") boolean wait);
}
